<?php

namespace App\Http\Controllers;

use App\Models\Institution;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Zone;
use App\Http\Requests\InstitutionRequest;

class InstitutionController extends CommonController
{
    /**
     *获取机构列表
     */
    public function getList()
    {
        $lists = Institution::with('zone')->orderBy('id', 'asc')->paginate(10);

        return view('institution.list')->with("lists", $lists)->with("actions", $this->actions);
    }

    /**
     * 获取单个机构详情
     */
    public function getDetail(Request $request)
    {
        $data = $request->input();
        $list = Institution::with('zone')->where('id', $data['id'])->first();

        return view('institution.detail')->with("list", $list);
    }

    /**
     * @param Request $request
     * 执行一个搜索
     */
    public function search(Request $request)
    {
        $data = $request->input();
        $lists = Institution::with('zone')->orderBy('id', 'asc');
        if (isset($data['subject']) && $data['subject'] != '') {
            $lists->where('subject', 'like', '%' . $data['subject'] . '%');
        }

        if (isset($data['represent']) && $data['represent'] != '') {
            $lists->where('represent', 'like', '%' . $data['represent'] . '%');
        }

        if (isset($data['type']) && $data['type'] != '') {
            $lists->where('type', $data['type']);
        }

        $lists = $lists->paginate(10);

        return view('institution.search')->with("lists", $lists)->with("subject", $data['subject'])
            ->with("represent", $data['represent'])->with("type", $data['type'])->with("actions", $this->actions);
    }

    /**
     * 获取创建页面
     */
    public function getCreate()
    {
        $zones = Zone::cache(1);
        $zoneTree = toTree($zones, 'id', 'upid', 'children');

        return view('institution.create')->with('zoneTree', $zoneTree);
    }

    /**
     * 执行一个创建
     */
    public function postCreate(InstitutionRequest $request)
    {
        $data = $request->input();
        unset($data['_token'], $data['formType'], $data['province_id']);

        $result = Institution::firstOrCreate($data);
        if (is_object($result)) {
            return AjaxCallbackMessage('机构新增成功,新机构的id为' . $result->id, true, url('institution/list'));
        } else {
            return AjaxCallbackMessage('机构新增失败,未知错误', false);
        }
    }

    /**
     *获取编辑页
     */
    public function getEdit(Request $request)
    {
        $zones = Zone::cache(1);
        $zoneTree = toTree($zones, 'id', 'upid', 'children');
        $data = $request->input();
        $list = Institution::with('zone')->where('id', $data['id'])->first();
        $family = familyTree($zones, $list->zone_id);


        return view('institution.edit')->with('list', $list)->with('zoneTree', $zoneTree)->with('family', $family);
    }

    /**
     * 执行一个编辑
     */
    public function postEdit(InstitutionRequest $request)
    {
        $data = $request->input();
        unset($data['_token'], $data['formType'], $data['province_id']);
        $result = Institution::where('id', $data['id'])->update($data);

        if ($result) {
            return AjaxCallbackMessage('成功修改' . $result . '条机构信息', true, url('institution/list'));
        } else {
            return AjaxCallbackMessage('机构编辑失败，您没有修改任何信息或其他错误', false);
        }
    }
}
